package cn.bestick.cblog.servlet.admin;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.bestick.cblog.dmo.Article;
import cn.bestick.cblog.dto.ArticleDto;
import cn.bestick.cblog.dto.ArticleLiteDto;
import cn.bestick.cblog.dto.CategoryDto;
import cn.bestick.cblog.service.ArticleService;
import cn.bestick.cblog.service.CategoryService;
import cn.bestick.cblog.service.impl.ArticleServiceImpl;
import cn.bestick.cblog.service.impl.CategoryServiceImpl;
import cn.bestick.cblog.util.PictureUtil;
import cn.bestick.cblog.util.PropertiesUtil;
import cn.bestick.cblog.util.StringUtil;

public class ArticleManageServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private ArticleService articleService = null;
	private CategoryService categoryService = null;

	public ArticleManageServlet() {
		articleService = new ArticleServiceImpl();
		categoryService = new CategoryServiceImpl();
	}

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String action = request.getParameter("action");
		if ("preSave".equals(action)) {
			preSave(request, response);
		} else if ("save".equals(action)) {
			save(request, response);
		} else if ("show".equals(action)) {
			show(request, response);
		} else if ("delete".equals(action)) {
			delete(request, response);
		} else {
			list(request, response);
		}
	}

	private void show(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String articleId = request.getParameter("articleId");
		ArticleDto article = articleService.getArticle(Integer
				.parseInt(articleId));
		request.setAttribute("article", article);
		request.setAttribute("mainPage", "template/article/manage-show.jsp");
		request.getRequestDispatcher("manage.jsp").forward(request, response);
	}

	private void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<ArticleLiteDto> articles = articleService.getArchive();
		request.setAttribute("articles", articles);
		request.setAttribute("mainPage", "template/article/manage-list.jsp");
		request.getRequestDispatcher("manage.jsp").forward(request, response);
	}

	private void preSave(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String imageBasePath = request.getSession().getServletContext()
				.getRealPath("image");
		String[] articleImages = PictureUtil.getPictures(imageBasePath,
				PropertiesUtil.getValue("articlePicturePath"));

		String articleId = request.getParameter("articleId");
		if (StringUtil.isNotEmpty(articleId)) {
			ArticleDto article = articleService.getArticle(Integer
					.parseInt(articleId));
			request.setAttribute("article", article);
		}
		
		List<CategoryDto> categories = categoryService.getCategories();
		
		request.setAttribute("categories", categories);
		request.setAttribute("pictures", articleImages);
		request.setAttribute("mainPage", "template/article/manage-save.jsp");
		request.getRequestDispatcher("manage.jsp").forward(request, response);
	}

	private void save(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String remark = request.getParameter("remark");
		String categoryId = request.getParameter("categoryId");
		String articleId = request.getParameter("articleId");
		String picture = request.getParameter("picture");
		System.out.println(content);

		Article article = new Article();

		article.setTitle(title);
		article.setContent(content);
		article.setRemark(remark);
		article.setCategoryId(Integer.parseInt(categoryId));
		article.setPicture(picture);
		article.setIsDraft(0);
		if (StringUtil.isEmpty(articleId)) {
			articleService.saveArticle(article);
		}else{
			System.out.println(articleId);
			article.setId(Integer.parseInt(articleId));
			articleService.updateArticle(article);
		}

		request.getRequestDispatcher("articleManage?action=list").forward(
				request, response);
	}

	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String articleId = request.getParameter("articleId");

		articleService.deleteArticle(Integer.parseInt(articleId));
		request.getRequestDispatcher("articleManage?action=list").forward(
				request, response);
	}
}
